﻿using BoriPeter.FatSecret;
using BoriPeter.FitnessJournal8.Models;
using BoriPetr.OAuth;
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BoriPeter.FitnessJournal8.ViewModels
{
    public class RootViewModel : ViewModelBase
    {
        #region Constructor
        public RootViewModel(INavigationService navigationService)
            : base(navigationService)
        {

        }
        #endregion

        #region History
        private ObservableCollection<Food> history;
        public ObservableCollection<Food> History
        {
            get
            {
                return this.history;
            }

            set
            {
                this.history = value;
                NotifyOfPropertyChange("History");
            }
        }
        #endregion

        #region DailyEntries
        private List<DailyEntry> dailyEntries;
        public List<DailyEntry> DailyEntries
        {
            get
            {
                return this.dailyEntries;
            }

            set
            {
                this.dailyEntries = value;
                NotifyOfPropertyChange("DailyEntries");
                DailyEntriesWeight.Merge(value.Where(de => de.WeightKg != 0)
                    .OrderByDescending(e => e.Date)
                    .Take(GridNumber));
                
                DailyEntriesIntake.Merge(value.Where(de => de.CaloriesIntakeKcal != 0)
                    .OrderByDescending(e => e.Date)
                    .Take(GridNumber));
                
                DailyEntriesCalories.Merge(value.Where(de => de.CaloriesBurnedKcal != 0)
                    .OrderByDescending(e => e.Date)
                    .Take(GridNumber));
            }
        }
        #endregion

        #region DailyEntriesWeight
        private ObservableCollection<DailyEntry> dailyEntriesWeight;
        public ObservableCollection<DailyEntry> DailyEntriesWeight
        {
            get
            {
                if (dailyEntriesWeight == null)
                    dailyEntriesWeight = new ObservableCollection<DailyEntry>();
                return this.dailyEntriesWeight;
            }

            set
            {
                this.dailyEntriesWeight = value;
                NotifyOfPropertyChange("DailyEntriesWeight");
            }
        }
        #endregion

        #region SelectedDailyEntriesWeight
        private DailyEntry selectedDailyEntriesWeight;
        public DailyEntry SelectedDailyEntriesWeight
        {
            get
            {
                return this.selectedDailyEntriesWeight;
            }

            set
            {
                this.selectedDailyEntriesWeight = value;
                NotifyOfPropertyChange("SelectedDailyEntriesWeight");
                Navigate2DailyEntry(value);
            }
        }
        #endregion

        #region DailyEntriesIntake
        private ObservableCollection<DailyEntry> dailyEntriesIntake;
        public ObservableCollection<DailyEntry> DailyEntriesIntake
        {
            get
            {
                if (dailyEntriesIntake == null)
                    dailyEntriesIntake = new ObservableCollection<DailyEntry>();
                return this.dailyEntriesIntake;
            }

            set
            {
                this.dailyEntriesIntake = value;
                NotifyOfPropertyChange("DailyEntriesIntake");
            }
        }
        #endregion

        #region SelectedDailyEntriesIntake
        private DailyEntry selectedDailyEntriesIntake;
        public DailyEntry SelectedDailyEntriesIntake
        {
            get
            {
                return this.selectedDailyEntriesIntake;
            }

            set
            {
                this.selectedDailyEntriesIntake = value;
                NotifyOfPropertyChange("SelectedDailyEntriesIntake");
                Navigate2DailyEntry(value);
            }
        }
        #endregion

        #region DailyEntriesCalories
        private ObservableCollection<DailyEntry> dailyEntriesCalories;
        public ObservableCollection<DailyEntry> DailyEntriesCalories
        {
            get
            {
                if (dailyEntriesCalories == null)
                    dailyEntriesCalories = new ObservableCollection<DailyEntry>();
                return this.dailyEntriesCalories;
            }

            set
            {
                this.dailyEntriesCalories = value;
                NotifyOfPropertyChange("DailyEntriesCalories");
            }
        }
        #endregion

        #region SelectedDailyEntriesCalories
        private DailyEntry selectedDailyEntriesCalories;
        public DailyEntry SelectedDailyEntriesCalories
        {
            get
            {
                return this.selectedDailyEntriesCalories;
            }

            set
            {
                this.selectedDailyEntriesCalories = value;
                NotifyOfPropertyChange("SelectedDailyEntriesCalories");
                Navigate2DailyEntry(value);
            }
        }
        #endregion

        #region OnInitialize()
        protected override async void OnInitialize()
        {
            base.OnInitialize();

            AuthenticationManager authentiacationManager;
            var fs = new FatSecretSettings("26c85e8ac61c4236aea846dccb645fa8", "caf38dead1db47269be416a06e81f25d");
            authentiacationManager = new AuthenticationManager(fs);
            OAuthToken accessToken;
            if (authentiacationManager.IsAuthenticated == false)
            {
                accessToken = await authentiacationManager.Authenticate();
            }
            else
            {
                accessToken = authentiacationManager.AccessToken;
            }
            FatSecretCommon.AccessToken = accessToken;
            FatSecretCommon.Settings = authentiacationManager.Settings;

            var historyList = await NutritionSource.GetListAsync(DAL.FoodService.History);
            History = new ObservableCollection<Food>(historyList);

            DailyEntries = await NutritionSource.GetDailyEntryListAsync(DateTime.Now.AddMonths(-1), DAL.EntryService.Weight);

            DailyEntries = await NutritionSource.GetDailyEntryListAsync(DateTime.Now.AddMonths(-1), DAL.EntryService.CaloriesIntake);

            DailyEntries = await NutritionSource.GetDailyEntryListAsync(DateTime.Now.AddMonths(-1), DAL.EntryService.CaloriesBurned);

            ProgressBarIsIndeterminate = false;
        }
        #endregion

        #region SelectedFood
        private Food selectedFood;
        public Food SelectedFood
        {
            get
            {
                return this.selectedFood;
            }

            set
            {
                this.selectedFood = value;
                Navigate2FoodDetails(value);
            }
        }
        #endregion

        #region Navigate2ToWeightsView()
        public void Navigate2ToWeightsView()
        {
            NavigationService.UriFor<WeightsViewModel>()
            .Navigate();
        }
        #endregion

        #region Navigate2EnergyBalance()
        public void Navigate2EnergyBalance()
        {
            NavigationService.UriFor<EnergyBalanceViewModel>()
            .Navigate();
        }
        #endregion

        #region Navigate2DailyIntake()
        public void Navigate2DailyIntake()
        {
            NavigationService.UriFor<DailyIntakeViewModel>()
            .Navigate();
        }
        #endregion

        #region Navigate2HistoryView()
        public void Navigate2HistoryView()
        {
            NavigationService.UriFor<SearchViewModel>()
            .WithParam(p => p.SearchExpression, "")
            .Navigate();
        }
        #endregion

        #region LoginSelected()
        public void LoginSelected()
        {
            ProgressBarIsIndeterminate = true;
            var fs = new FatSecretSettings("26c85e8ac61c4236aea846dccb645fa8", "caf38dead1db47269be416a06e81f25d");
            AuthenticationManager authentiacationManager = new AuthenticationManager(fs);
            authentiacationManager.RemoveCredentials();
            NutritionSource.Reset();
            OnInitialize();
        }
        #endregion
    }
}
